//
//  HZOrderBuyerPayWayViewController.m
//  app
//
//  Created by 刘华舟 on 15/5/8.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "HZOrderBuyerPayWayViewController.h"
#import "HZRemoteResult.h"
#import "HZMallCartsHttpManager.h"
#import "NSDictionary+SafetyGet.h"

#import "MyToast.h"

#import "HZImageSourceConstant.h"

static const NSInteger kOrderBuyerMargin = 24;
static const NSInteger kOrderBuyerLabelHeight = 80;
static const NSInteger kOrderBuyerBackViewHeight = 600;
static const NSInteger kOrderBuyerButtonHeight = 80;

@interface HZOrderBuyerPayWayViewController()<UITextFieldDelegate>



@property (strong, nonatomic) UIView *backView;


@property (strong, nonatomic) NSArray *btnArray;
@property (strong, nonatomic) UIButton *lastBtn;

@property (strong, nonatomic) UIButton *backBtn;
@property (strong, nonatomic) UIButton* comfirmBtn;

@end

@implementation HZOrderBuyerPayWayViewController


- (void)viewDidLoad{
    [super viewDidLoad];
    
    self.title = @"发票信息";
    
    self.view.backgroundColor = HZ_MALL_M_LIGHT_GRAY_BACKGROUND_COLOR;
    
    //backBtn
    self.backBtn = [[UIButton alloc] initWithFrame:CGRectOf([self.ubo fx:UB_LEFT fy:UB_TOP],
                                                            [self.ubo uw:40 oh:40])];
    [self.backBtn setImage:[UIImage imageNamed:kImageMallArrowBackReturnRed] forState:UIControlStateNormal];
    self.backBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [self.backBtn addTarget:self action:@selector(backBtnOnClick) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.backBtn];
    
    //初始化背景图
    self.backView = [[UIView alloc] initWithFrame:CGRectOf([self.view.ufo fx:UB_LEFT fy:UB_TOP uy:2*kOrderBuyerMargin oy:self.navigationAndStatusBarHeight],
                                                           [self.view.ubo fw:UB_WIDTH uh:kOrderBuyerBackViewHeight])];
    self.backView.backgroundColor = HZ_WHITE_COLOR;
    
    [self.view addSubview:self.backView];
    
    //backBtn
    self.comfirmBtn = [[UIButton alloc] initWithFrame:CGRectOf([self.backView.ufo fx:UB_LEFT ux:kSizeMallViewMargin*2 fy:UB_BOTTOM oy:kSizeMallViewMargin/2],
                                                            [UBO_WINDOW fw:UB_WIDTH uw:-2*(kSizeMallViewMargin*2)oh:kSizeMallViewMargin*2])];
    [self.comfirmBtn setTitle:@"确定" forState:UIControlStateNormal];
    [self.comfirmBtn setTitleColor:HZ_WHITE_COLOR forState:UIControlStateNormal];
    [self.comfirmBtn setBackgroundColor:HZ_MALL_MAIN_THEME_RED];
    [self.comfirmBtn.titleLabel setFont:kHZFont_Content_L];
    [self.comfirmBtn addTarget:self action:@selector(comfirmBtnOnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.comfirmBtn];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self createPayHabitView];
}

- (void)createPayHabitView
{
    
    NSArray * btnTitleArray = self.data.pay;
    
    NSInteger payHabitViewHeight = 60 + 60*((btnTitleArray.count+1)/2);
    
    self.backView.frame = CGRectMake(0, self.navigationAndStatusBarHeight+20, SCREEN_WIDTH, payHabitViewHeight);
    
    [self.view addSubview:self.backView];
    
    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, SCREEN_WIDTH-20, 20)];
    
    label.text = @"支付方式";
    [label setFont:JKAppGlobleFontMenu];
    [self.backView addSubview:label];
    
    CGFloat width = (SCREEN_WIDTH - 20*3)*0.5;
    CGFloat height = 40;
    NSMutableArray *btnArr = [NSMutableArray array];
    NSInteger row = 0;
    for (int i = 0; i < btnTitleArray.count; i++) {
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        if (i%2 == 0) {
            row ++;
        }
        btn.frame = CGRectMake(20*(i+1)+i*width, label.ufo.yy+20+(row-1)*60, width, height);
        [btn setTitle:[btnTitleArray[i] stringForKey:APIKEY_mall_name nilValue:@""] forState:UIControlStateNormal];
        btn.titleLabel.font = JKAppGlobleFontSmallTagText;
        [btn setTitleColor:HZ_MALL_TEXT_L_LIGHTGRAY_COLOR forState:UIControlStateNormal];
        btn.layer.borderWidth = UP(1);
        [btn addTarget:self action:@selector(payHabitBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.backView addSubview:btn];
        
        btn.tag = [btnTitleArray[i] integerForKey:APIKEY_mall_type nilValue:-1];

        if ([self.data.payTypeNum isEqualToString:[btnTitleArray[i] stringForKey:APIKEY_mall_type nilValue:@"0"]]) {
            btn.layer.borderColor = HZ_MALL_MAIN_THEME_ORANGE.CGColor;
            self.lastBtn = btn;
        }
        
        [btnArr addObject:btn];
        
    }
    
    self.comfirmBtn.frame = CGRectOf([self.backView.ufo fx:UB_LEFT ux:kSizeMallViewMargin*2 fy:UB_BOTTOM oy:kSizeMallViewMargin/2],
                                                               [self.ubo fw:UB_WIDTH uw:-2*2*kSizeMallViewMargin oh:kSizeMallViewMargin*2]);

}



- (void)payHabitBtnClick:(UIButton *)btn
{
    if (btn == self.lastBtn) {
        return;
    }
    self.lastBtn.layer.borderColor = HZ_MALL_BORDER_LINE_GRAY_COLOR.CGColor;
    
    self.lastBtn = btn;
    
    self.lastBtn.layer.borderColor = HZ_MALL_MAIN_THEME_ORANGE.CGColor;
    
}


- (void)comfirmBtnOnClick{
    
    if (self.lastBtn == nil) {
        return;
    }
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(finishOrderBuyerPayWaySetting)]) {
        
        if (self.lastBtn.tag == -1) {
            return;
        }
        
        self.data.payTypeNum = [NSString stringWithFormat:@"%d", self.lastBtn.tag];
        self.data.payTypeName = self.lastBtn.currentTitle;
        
        [self.delegate finishOrderBuyerPayWaySetting];
        
        [self backBtnOnClick];
    }
}

- (void)backBtnOnClick{

    [self.navigationController popViewControllerAnimated:YES];
    
    
}



@end
